The FZERO Function
The fzero
function tries to find a root of a function near
an initial value that you supply.
First, define a function that can be passed as an argument to
the fzero
function. Then, call fzero
to find the roots.
>> fzero( @my_function, x0 )
The above command finds the x value that is nearest to the value in x0
that will cause the function computed by my_function
to return a value of zero.
The fzero
function can also be called with an anonymous function that
represents the function to be evaluated, instead of defining a new function in a separate m-file.
>> fzero( @(x)-x^2+10 , 3 )
The fzero
function can also be called with a range of x values that
limits the range to search for a valid root.
>> fzero( @(x)-x^2+10 , [3 5] )
The fzero
function can also be called to find the root of a polynomial
using the polyval
command.
Use the following command to find the root of :
>> fzero( @(x)polyval([1 2 3 4],x), 0 )
It is also possible to find the root of a function that requires more
than one input, if only one of the inputs to the function is a variable.
For example, let's say we want to find where a polynomial crosses the
x-axis and the coefficients of our polynomial are stored in the vector c
.
The command to evaluate the polynomial for a value in x
is polyval(c,x)
. We can find the roots of this polynomial
with this syntax:
>> fzero( @(x)polyval(c,x), x0 )
The @(x)
piece indicates that we wish to treat x
as the variable in the function polyval(c,x)
. The x0
argument is still the initial guess for the root of this function.
Note: Be sure that c
has been assigned its desired
value before executing this command.